"use strict";
cc._RF.push(module, 'eea0clnIJJAUpky0Dpdk7nv', 'Game');
// Script/Game.js

"use strict";

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

//var MoveBg = require("BgMove");
var HeroPlayer = require("Fly");
var Bullet = require("Bullet");
var x = 0;
var y = 0;
var isTouch = false;
var self = void 0;
var AU = require("AudioUtils");
var game = cc.Class({
  extends: cc.Component,

  properties: {
    // bgsprite1 节点，用于背景移动
    bgsprite1: {
      default: null,
      type: cc.Node
    },
    // bgsprite2 节点，用于背景移动
    bgsprite2: {
      default: null,
      type: cc.Node
    },
    // score label 的引用
    scoredisplay: {
      default: null,
      type: cc.Label
    },

    //我方飞机节点
    hero: {
      default: null,
      type: cc.Node
    },

    //敌方飞机预制模板
    enemy: {
      default: null,
      type: cc.Prefab
    },

    zidanprefab: { //子弹模板
      default: null,
      type: cc.Prefab
    },

    badzidanprefab: { //敌军子弹模块
      default: null,
      type: cc.Prefab
    },

    goldenprefab: { //金币
      default: null,
      type: cc.Prefab
    },

    goldnum: { //金币数量
      default: null,
      type: cc.Label
    },

    diamondnum: { //钻石数量
      default: null,
      type: cc.Label
    },

    blockinputs: { //阵亡界面
      default: null,
      type: cc.Node
    },

    jn1prefab: { //技能1本体动画效果
      default: null,
      type: cc.Prefab
    },

    jn1iconprefab: { //技能1图标
      default: null,
      type: cc.Prefab
    },

    hd1iconprefab: { //护盾1图标
      default: null,
      type: cc.Prefab
    }
  },

  setEventControl: function setEventControl() {
    this.node.on(cc.Node.EventType.TOUCH_MOVE, this.mouseFun, this);
    this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this, true);
  },

  //用户touch屏幕时,发射子弹
  onTouchStart: function onTouchStart() {
    AU.inst.setStopEffect(EFFECT_BULLET_SHOT1);
    AU.inst.playEffect(EFFECT_BULLET_SHOT1, true);
    isTouch = true;
    this.schedule(this.creatrZidanSchedule, 0.15);
    /*this.creatrZidanSchedule = function(){
    	// 这里的 this 指向 component
      this.createNewZidan();
    }*/
  },

  onTouchEnd: function onTouchEnd() {
    AU.inst.setStopEffect(EFFECT_BULLET_SHOT1);
    //this.nodePos = this.node.getPosition(); //获取触摸结束之后的node坐标；
    x = 0; //x,y变量置零,防止鼠标点击玩家飞机直接移动到鼠标点击位置
    y = 0;
    //onsole.log(x+"---------"+y);
    isTouch = false;
    this.unschedule(this.creatrZidanSchedule);
  },

  mouseFun: function mouseFun(event) {
    var self = this;
    var touches = event.getTouches();

    //触摸时不断变更的位置
    var newPos = self.node.convertToNodeSpaceAR(touches[0].getLocation());

    if (x == 0 && y == 0) {
      x = newPos.x;
      y = newPos.y;
    }

    var prePos = new cc.Vec2(x, y);
    x = newPos.x;
    y = newPos.y;

    var subPos = prePos.sub(newPos); // 2.X版本是 p1.sub(p2);


    //////////////边界控制
    var minX = -self.node.parent.width / 2 - self.node.width / 2; //最小X坐标；
    var maxX = Math.abs(minX);
    var minY = -self.node.parent.height / 2 - self.node.height / 2; //最小Y坐标；
    var maxY = Math.abs(minY);
    this.hero.x = this.hero.x - subPos.x;
    this.hero.y = this.hero.y - subPos.y;
    if (this.hero.x < minX) {
      this.hero.x = minX;
    };
    if (this.hero.x > maxX) {
      this.hero.x = maxX;
    };
    if (this.hero.y < minY) {
      this.hero.y = minY;
    };
    if (this.hero.y > maxY) {
      this.hero.y = maxY;
    };
  },


  createNewZidan: function createNewZidan() {
    var heroplay = this.hero.getComponent(HeroPlayer); //角色绑定控件
    var level = heroplay.level;
    for (var a = 0; a < level; a++) {

      // 使用给定的模板在场景中生成一个新节点
      var newZd = cc.instantiate(this.zidanprefab);
      newZd.getComponent("Bullet").powerNum = 1;
      // 将新增的节点添加到 Canvas 节点下面
      this.node.addChild(newZd);
      this.node.zIndex = 3;
      // 为子弹设置位置
      newZd.setPosition(cc.v2(this.hero.x + (-(level - 1) / 2 * newZd.width + a * newZd.width), this.hero.y + this.hero.height / 2));
    }
  },

  /**
   * 创建敌机子弹
   */
  createNewBadZidan: function createNewBadZidan(badFly) {
    // 使用给定的模板在场景中生成一个新节点
    var newBadZd = cc.instantiate(this.badzidanprefab);
    newBadZd.getComponent("Bullet").powerNum = 1;
    // 为子弹设置位置
    newBadZd.setPosition(cc.v2(badFly.x, badFly.y - badFly.height / 2));
    newBadZd.zIndex = 3;
    // 将新增的节点添加到 Canvas 节点下面
    this.node.addChild(newBadZd);
  },
  /**
   * 创建敌机
   */
  createNewBadFly: function createNewBadFly() {
    var winSize = cc.winSize;
    var newBadFly = cc.instantiate(this.enemy);
    newBadFly.flyTime = 2;
    newBadFly.flyLength = 1000;
    // 为敌机设置位置
    newBadFly.setPosition(cc.v2(Math.floor(Math.random() * winSize.width) - winSize.width / 2, 450));
    newBadFly.zIndex = 3;
    this.node.addChild(newBadFly);
    this.createNewBadZidan(newBadFly);
  },

  /**
   * 产生爆照金币
   * @param {Object} r 爆炸圆半径
   * @param {Object} bCount 产生金币数量
   * @param {Object} bX 爆炸位置x坐标
   * @param {Object} bY 爆炸位置y坐标
   */
  createNewGolden: function createNewGolden(r, bCount, bX, bY) {
    //  	console.log("bx:"+bX+"--------by:"+bY);
    var radians = Math.PI / 180 * Math.round(360 / bCount); //弧度
    for (var i = 0; i < bCount; i++) {
      var x_ = 0 + r * Math.sin(radians * i);
      var y_ = 0 + r * Math.cos(radians * i);
      //	        console.log("x:"+x_+"--------y:"+y_);
      var golden = cc.instantiate(self.goldenprefab);
      golden.setPosition(cc.v2(bX, bY)); //金币产生的位置为当前敌机爆炸的位置
      var gold = golden.getComponent("Golden");
      gold.endX = x_;
      gold.endY = y_;
      gold.flyTime = 0.6;
      self.node.addChild(golden);
      //self.node.zIndex = 999;
    }
  },

  /**
   * 创建技能图标道具
   */
  createJinengProp: function createJinengProp() {
    var winSize = cc.winSize;

    var jineng = cc.instantiate(self.jn1iconprefab);
    jineng.setPosition(cc.v2(0, winSize.height));
    self.node.addChild(jineng);

    var hudun = cc.instantiate(self.hd1iconprefab);
    hudun.setPosition(cc.v2(0, winSize.height));
    self.node.addChild(hudun);
  },

  /**
   * 创建技能本体
   */
  createJinengAnim: function createJinengAnim() {
    var jineng = cc.instantiate(self.jn1prefab);
    jineng.setPosition(cc.v2(0, 0));
    self.node.addChild(jineng);
  },

  /**
   * 显示护盾
   */
  showProtect1: function showProtect1() {
    var protectc = cc.find('Canvas/fly/protectc');

    self.unschedule(self.hideProtect1); //每次显示护盾之前先移除 '护盾消失定时器'hideProtect1,避免身上有护盾的情况下吃到护盾道具时不能重置护盾时长,避免新护盾被上一个护盾定时器提前取消
    self.unschedule(self.protectBlink);

    protectc.stopAllActions();

    protectc.opacity = 255;
    protectc.active = true;

    self.scheduleOnce(self.protectBlink, 5);
    self.scheduleOnce(self.hideProtect1, 8);
  },

  /**
  * 取圆上 count 等分 坐标
  * @param {Object} r 半径
  * @param {Object} ox 圆心x坐标
  * @param {Object} oy 圆心y坐标
  * @param {Object} count 份数
  */
  getPoint: function getPoint(r, ox, oy, count) {
    var radians = Math.PI / 180 * Math.round(360 / count),
        //弧度
    i = 0; //这for循环我也是服了
    for (; i < count; i++) {
      var x = ox + r * Math.sin(radians * i);
      var y = oy + r * Math.cos(radians * i);
      point.unshift({ x: x, y: y }); //为保持数据顺时针
    }
  },

  /**
   * 背景移动
   */
  setBgMoveCreate: function setBgMoveCreate() {
    //如果背景1的坐标移出屏幕开始设置新的坐标
    if (this.bgsprite1.y < -796) {
      this.bgsprite1.setPosition(0, 796);
    }
    //如果背景2的坐标移出屏幕开始设置新的坐标
    if (this.bgsprite2.y < -796) {
      this.bgsprite2.setPosition(0, 796);
    }
  },

  onLoad: function onLoad() {
    self = this;
    this.node.runAction(cc.fadeIn(1.0));
    cc.director.getCollisionManager().enabled = true; //开启碰撞系统
    self = this;
    //cc.director.getCollisionManager().enabledDebugDraw = true;//开启绘制区域
    this.setEventControl();
    this.creatrZidanSchedule = function () {
      // 这里的 this 指向 component
      this.createNewZidan();
    };
    this.creatrBadFlySchedule = function () {
      // 这里的 this 指向 component
      this.createNewBadFly();
    };
    this.schedule(this.creatrBadFlySchedule, 1);

    this.createJinengSchedule = function () {
      // 这里的 this 指向 component
      this.createJinengProp();
    };
    this.schedule(this.createJinengSchedule, 5);

    this.protectBlink = function () {
      //设置防护罩8秒后消失
      var protectc = cc.find('Canvas/fly/protectc');
      protectc.runAction(cc.repeat(cc.sequence(cc.fadeTo(0.1, 0), cc.fadeTo(0.1, 255)), 3 / 0.1));
    };
    this.hideProtect1 = function () {
      var protectc = cc.find('Canvas/fly/protectc');
      protectc.active = false;
    };
    this.showProtect1(); //初始护盾
  },
  start: function start() {},
  update: function update(dt) {
    this.setBgMoveCreate();
  }
});
game.inst = new game();

cc._RF.pop();